package com.example.androidxdemo.viewModel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class LoginViewModel : ViewModel() {
    private val defaultUsername = "a"
    private val defaultPassword = "a"
    var needPopUpTo = MutableLiveData(false)

    enum class AuthenticationState {
        UNAUTHENTICATED,        // Initial state, the user needs to authenticate
        AUTHENTICATED,        // The user has authenticated successfully
        INVALID_AUTHENTICATION,  // Authentication failed
        CANCEL_AUTHENTICATION
    }
    var authenticationState = MutableLiveData<AuthenticationState>()

    init {
        authenticationState.value = AuthenticationState.UNAUTHENTICATED
    }

    fun login(username: String, password: String) {
        authenticationState.value = if (username == defaultUsername && password == defaultPassword) {
            AuthenticationState.AUTHENTICATED
        } else {
            AuthenticationState.INVALID_AUTHENTICATION
        }
    }

    fun logout() {
        authenticationState.value = AuthenticationState.UNAUTHENTICATED
    }
    fun setUnauthenticated() {
        authenticationState.value = AuthenticationState.UNAUTHENTICATED
    }
    fun resetPopUpTo() {
        needPopUpTo.value = false
    }
    fun logout(needPopUpTo :Boolean) {
        this.needPopUpTo.value = needPopUpTo
        authenticationState.value = AuthenticationState.CANCEL_AUTHENTICATION
    }
}